home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9283 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: news.voicenet.com!news
  2. From: kobak@voicenet.com (Peter Kobak)
  3. Newsgroups: comp.lang.c++
  4. Subject: Forward declarations of typedefs
  5. Date: 29 Feb 1996 18:30:13 GMT
  6. Organization: Voicenet - Internet Access - (215)674-9290
  7. Message-ID: <4h4rbl$or7@news.voicenet.com>
  8. References: <4h2fhp$si8@elaine22.Stanford.EDU>
  9. Reply-To: kobak@voicenet.com
  10. NNTP-Posting-Host: ivyland335.voicenet.com
  11. X-Newsreader: NeoLogic News for OS/2 [version: 4.5 YK Beta]
  12.  
  13. In message <4h2fhp$si8@elaine22.Stanford.EDU> -
  14. iburrell@leland.Stanford.EDU (Ian Burrell) writes:
  15.  
  16. :>I am trying to do a forward declaration of a type defined in another
  17. :>header so that I can make a class member that is a pointer.  However,
  18. :>if the forward declaration type is later defined as a typedef, I get
  19. :>lots of errors.
  20.  
  21. This is an easy mistake to make when moving from C to C++. Despite the
  22. name, typedef does _not_ define a new type. It's a lot more like a
  23. #define. When you typedef against a name which was previously declared
  24. to be a class, you'll always get errors because typedef != class. It's
  25. like declaring "int a", then later declaring "typedef int a"; they're
  26. different things.
  27.  
  28. IMHO, it's best to avoid typedef in C++ altogether, except when you
  29. genuinely need a symbolic renaming. An example of this is the use of a
  30. typedef to rename a template instantiation:
  31.  
  32. typedef CollectionClass<MyThing> MyThingCollection;
  33.  
  34. In this case, I have _not_ created a new class MyThingCollection. I can
  35. now refer to the class CollectionClass<MyThing> equivalently as
  36. MyThingCollection. Again, it's best to think of it like a define:
  37.  
  38. #define MyThingCollection CollectionClass<MyThing>
  39.  
  40. ================
  41. Peter Kobak
  42. kobak@voicenet.com
  43.  
  44.  
  45.